<?php
declare(strict_types=1);
/**
 * Author: Traveler
 * Time  : 2021/8/31 14:31
 */

namespace sunshine\exceptions;


use RuntimeException;
use Throwable;

/**
 * API异常类
 *
 * @package sunshine\exceptions
 */
class ApiException extends RuntimeException
{
    /**
     * http状态码
     *
     * @var int
     */
    private $statusCode;

    /**
     * http头信息
     *
     * @var array
     */
    private $headers;

    /**
     * 构造函数
     *
     * @param array          $errorCode  API错误代码数组，[0]为错误代码，[1]为错误信息
     * @param int            $statusCode http状态码
     * @param array          $headers    http头信息
     * @param Throwable|null $previous   用于异常链接的上一个throwable
     */
    public function __construct(array $errorCode, int $statusCode = 400, array $headers = [], Throwable $previous = null)
    {
        [$code, $message] = $errorCode;
        $this->statusCode = $statusCode;
        $this->headers    = $headers;

        parent::__construct($message, $code, $previous);
    }

    /**
     * 获取http状态码
     *
     * @return int
     */
    public function getStatusCode(): int
    {
        return $this->statusCode;
    }

    /**
     * 获取http头信息
     *
     * @return array
     */
    public function getHeaders(): array
    {
        return $this->headers;
    }
}